package com.xc.controller;

import com.alibaba.fastjson.JSON;
import com.xc.pojo.Account;
import com.xc.service.AccountService;
import com.xc.utils.SysConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 注册
     * @param account
     * @return
     */
    @ResponseBody
    @RequestMapping("/register")
    public String register(Account account){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (accountService.addAccount(account)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"恭喜您，注册成功！");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"很遗憾，注册失败！");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询是否存在该用户名
     * @param loginName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkName")
    public String checkName(String loginName){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (accountService.queryAccountByName(loginName)!=null){
            map.put(SysConstants.EXIST,true);
            map.put(SysConstants.MESSAGE,"用户名已经存在，请重新输入！");
        }else {
            map.put(SysConstants.EXIST,false);
            map.put(SysConstants.MESSAGE,"用户名可用！");
        }
        return JSON.toJSONString(map);
    }
}
